package com.yitong.blk.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.yitong.blk.domain.TestUser;

@Repository
public class TestUserDaoImpl {
	
	@Autowired
	@Qualifier("secondJdbcTemplate")
    private JdbcTemplate jdbcTemplate;
	
	@Transactional(readOnly = true)
	public TestUser findTestByName(String name) {
		return jdbcTemplate.queryForObject("select c.* from TestUser c where c.name=?", new Object[]{name}, new TestRowMapper());
	}
	
	@Transactional(readOnly = true)
    public List<TestUser> getPageList(int start,int end) {
        return jdbcTemplate.query("select id,name,age from ( select row_.*, rownum rownum_ from TestUser row_ where rownum <= ? ) where rownum_ > ?",new Object[]{end,start},new TestRowMapper());
    }

}

class TestRowMapper implements RowMapper<TestUser>{
	@Override
	public TestUser mapRow(ResultSet rs, int rowNum) throws SQLException {
		TestUser t = new TestUser();
		t.setId(rs.getString("id"));
		t.setName(rs.getString("name"));
		t.setAge(rs.getString("age"));
		return t;
	}
}